package com.vgemv.jsutilitysdk.log;
import android.content.Context;
import android.graphics.Color;
import android.widget.TextView;

import com.vgemv.jsutilitysdk.file.JsFileManager;
import com.vgemv.jsutilitysdk.log.target.JsLogTargetView;

import java.util.ArrayList;
import java.util.Date;



/**
 * 有两颜色:
 * mColor_historyLog;
 * mClolor_newLog;
 *
 * 满足以下条件之一的消息,用mColor_newMessage来显示
 * 1,最新的mint_newLogCount个日志
 * 2,消息距离当前显示时间<=mDate_logInterval之
 */

/**
 * 1,需不需要有多个实例对象.
 * 每个LogManager目前只关联一个文件记录器,当需要两个文档保存两个不同文件时,需要两个对象.
 * 让LogManger管理2个文件记录器,还是用2个Manager,各自管理1个.
 * 选择后者吧.
 *
 */

/**
 * 废弃需求记录:
 * 1,维护一个Tag列表.
 * 2,可以配置显示哪些tag的信息. 不需要.  这些操作可以得到日志后,自己操作.不需要再记录库中实现.
 * 3,将日志输出到window上. 结论:不需要这么大的权限.并且也不美观. 输出到指定的view上就可以了.
 *
 * 单例还是类方法.
 * 1,如果确保只需要一个对象,使用类方法吧.简单一点.
 *
 */


public class JsLogManager {

    private static ArrayList<JsLogModel> mArrayList_logModel;
    private static boolean mb_isWindowLoggerEnable;
    private static int mint_newLogCount = 1;
    //判断为新日志的时间间隔
    private static int mint_newLogTimeInterval = 3;
    private static int mint_historyLogColor = Color.argb(255, 255, 255, 255);
    private static int mint_newLogColor = Color.argb(255, 255, 200, 200);
    private static int mint_currentLogColor = Color.argb(255, 255, 0, 0);
    private static Date mDate_now;
    private static Boolean mb_isInitialed = false;
    //默认保存30份文件.
    private static int mi_logFileCount = 30;

    private JsLogger mLogger;

    //LogLevel
    public static final int JsLogLevelVerbose = 0;
    public static final int JsLogLevelDebug = 1;

    public static final int JsLogLevelInfo = 2;
    public static final int JsLogLevelWarn = 3;

    public static final int JsLogLevelError = 4;
    public static final int JsLogLevelFatal = 5;

    public static final int JsLogLevelNone = 6;

    //LogTarget
    //console,file,view.
    public static final int JsLogTargetConsole = 1 << 0;
    public static final int JsLogTargetFile = 1 << 1;
    public static final int JsLogTargetView = 1 << 1;
    public static final int JsLogTargetAll = 0xffff;

    private static JsLogManager _sharedInstance = null;

    public static JsLogManager sharedInstance() {
        if(_sharedInstance == null) {
            _sharedInstance = new JsLogManager();
            String defaultLogDirPath = JsFileManager.sharedInstance().logDirPathInAppFilePath();
            _sharedInstance.mLogger = new JsLogger(defaultLogDirPath);
        }
        return _sharedInstance;
    }
    private JsLogManager() {

    }

    public JsLogManager(String logDirPath) {
        mLogger = new JsLogger(logDirPath);
    }
    public JsLogManager(String logDirPath,Context context) {
        mLogger = new JsLogger(logDirPath,context);

    }



    //增加初始化viewTarget的方法.
    public void initViewLogTarget(Context context) {
        mLogger.initViewLogTarget(context);
    }

    /*
    target:file,console
    level:verbose
     */
    public static void logfc_v(String context, String format, Object... args) {
        JsLogManager.sharedInstance().log(JsLogLevelVerbose,JsLogTargetFile|JsLogTargetConsole,context,format,args);
    }
    public static void logfc_e(String context, String format, Object... args) {
        JsLogManager.sharedInstance().log(JsLogLevelError,JsLogTargetFile|JsLogTargetConsole,context,format,args);
    }
    public static void logfc_f(String context, String format, Object... args) {
        JsLogManager.sharedInstance().log(JsLogLevelFatal,JsLogTargetFile|JsLogTargetConsole,context,format,args);
    }
    public static void logfc_i(String context, String format, Object... args) {
        JsLogManager.sharedInstance().log(JsLogLevelInfo,JsLogTargetFile|JsLogTargetConsole,context,format,args);
    }

    /**
     * target:console
     * level:debug
     */
    public static void logc_d(String context,String format,Object...args){
        JsLogManager.sharedInstance().log(JsLogLevelDebug,JsLogTargetConsole,context,format,args);
    }

    public static void logfc_d(String context,String format,Object...args){
        JsLogManager.sharedInstance().log(JsLogLevelDebug,JsLogTargetConsole|JsLogTargetFile,context,format,args);
    }


    public  void log(int logLevel,int logTarget, String context,String format, Object... args) {
        JsLogEvent logEvent = new JsLogEvent(String.format(format,args),logLevel,context,logTarget);
        mLogger.log(logEvent);
    }

    public void log(JsLogEvent logEvent) {
        mLogger.log(logEvent);
    }

    public  void setLogLevelFilter(int level) {
        mLogger.logLevelFilter = level;
    }


    public  void setContextKeysFilter(ArrayList<String> keys) {
        mLogger.logTagFilters = keys;
    }

    public JsLogTargetView getViewTarget() {
        return mLogger.mViewTarget;
    }


    //设置LogHome,存放日志文件.
    public  void setLogDirPath(String dirPath) {
        mLogger.mFileTarget.setLogDirPath(dirPath);
    }

    //获取当前日志文件夹.
    public  String getLogDirPath() {
        return mLogger.mFileTarget.getLogDirPath();
    }


    public  void printLogFileDir() {
        mLogger.mFileTarget.printLogDirPath();
    }

    //获取今日日志的文件夹
    public  String getTodayFilePath() {
        return mLogger.mFileTarget.currentLogFilePath();
    }
    public void setMaxLogFileCount(int count) {
        if(mi_logFileCount == count || count < 0) {
            return;
        }
        mi_logFileCount = count;
        limitFileCount();
    }
    //清空所有的文件夹.
    public  void clearLogDir() {
        JsFileManager.clearDir(mLogger.mFileTarget.currentLogFilePath(),false);
    }
    //清空当天的文件夹.
    public  void clearTodayFile() {
        mLogger.mFileTarget.clearFileContent();
    }

    public  void limitFileCount() {
        JsFileManager.limitDirFileCount(getLogDirPath(),mi_logFileCount);
    }

}
